Ext.define('Sencha.controller.InformationListController', {
    extend: 'Ext.app.Controller',

    config: {
        
        optimizedItems: null,
           
        refs: {
            main: 'informationnavigationview',
            //informationContainerView: 'informationcontainerview',
            informationListView: 'informationlistview',
           infoContainer: 'container[title=Information]'
           //infoContainer: 'container[cls=infocont]'
           
        },
        control: {
            'informationListView' :{
           		//initialize: 'onShowDictionaryDetailView',
                itemtap: 'onItemTap'
                
                //show: 'onDictionaryDetailViewShow'
            }/*,
            informationContainerView: {
                activate: 'onInfoContainerViewActivate'
            }*/,
            main: {
                //initialize: 'onInitializeNav',
                //activate: 'onInfoNavViewActivate',
                //deactivate: 'onInfoNavViewDeactivate'
            }    

            /*,
            'main' : {
                initialize: 'onInit',
                show: 'onShow',
                hide: 'onHide'
            }*/
        },
           
        // properties
        infoListViewRef: null
    },

    onInitializeNav: function() {
        console.log('in onInitializeNav *******************');
        //infoListItemRefs = new Array();  
    },
           
    onInfoNavViewDeactivate: function(nav) {
        //var items = nav.getItems().items;
        /*
        var items = nav.getItems().items;
        var i = 0;
        var iLen = items.length;
        var newItems = [];
        */
        //var items = nav.getItems().items,
        var items = nav.getInnerItems(),
           i = 0,
           iLen = items.length,
           newItems = [];
        
           
        console.log('iLen = ' + iLen );
           
        for (; i < iLen; i++) {
           console.log('remembering item ' + items[i].getTitle() );
           newItems.push(items[i]);
        }
           
        this.setOptimizedItems(newItems);
           
        nav.removeAll(false, false);   
        
           
           
           
        /*
        console.log('iin info deactivate...');
           
        // clear out infoListItemRefs array
        var len = optimizedItems.length;
        for (i=0; i<len; i++) {
           console.log('popping array');
           optimizedItems.pop();
        }
           
        // store all item ref's
        var itemCount =  this.getMain().getInnerItems().length;
        console.log('num items to remember =' + itemCount);
           
        for (i=0; i<itemCount; i++) {
           //infoListItemRefs[i] = this.getMain().getAt(i);
           optimizedItems
           //var temp = this.getMain().getInnerItems()[i];
           console.log('remembering ' + infoListItemRefs[i].getTitle() );
        }
           console.log('just before remove all ');
           //console.log('first item remembered is ' + temp.getTitle() );
           
        this.getMain().removeAll(false, true);
        */
    },
           
    onInfoNavViewActivate: function(nav) {
        var items = this.getOptimizedItems();
   
        if (!items) {
           console.log('no items');
           nav.push({xtype: 'informationlistview'});
           
           return;
        }
        
        console.log('after it. items=' + items.length);
        //console.log('item 1 = ' + items.getAt(0).getTitle() );
           
        //nav.push(items);
           len = items.length;
           for (i=0; i<len; i++) {
           console.log('pushing ' + items[i].getTitle() );
            nav.push( items[i] );
           }
    },
           
           
           
     /*      
    onInfoContainerViewActivate: function(container, newobj, oldobj, opts) {
        console.log('in onInfoContainerViewActivate...');
        //infoListRefs = new Array();
        var itemCount = infoListItemRefs.length;
           console.log('after itemCount assignment, itemCount=' + itemCount);
           
        // if first time
        if ( itemCount == 0 ) {
           // first time
            this.getMain().push({xtype: 'informationlistview'});
        } else {
           // restore all items
           
           console.log('not fist tiime. itemCount=' + itemCount);
           
           for (i=0; i<itemCount; i++) {
        
                console.log('restoring item ' + infoListItemRefs[i].getTitle() );
                var itemToRestore = infoListItemRefs[i];
                //this.getMain().push( [itemToRestore] );
           }
           this.getMain().push( infoListItemRefs );
        }
           
        
        
        //newobj.add({xtype: 'informationlistview'});
        //newobj.add({xtype: 'informationlistview'}).show();
        //this.getInformationContainerView().add({xtype: 'informationlistview'});
        //this.getInfoContainer().add({xtype: 'informationlistview'});
    },
      */
           
           
    onShow: function() {
        //this.getMain().add( this.getInfoListViewRef() );
        console.log('InformationListController.show()');
        //this.getMain().push( this.getInfoListViewRef() );
           this.getMain().add( this.getInfoListViewRef() );
        console.log('end of show, num items = ' + this.getMain().getItems().length);
    },
           
    onHide: function() {
        console.log('InformationListController.hide()');
        this.getMain().removeAt(1);
        console.log('end of hide, num items = ' + this.getMain().getItems().length);
    },

    onInit: function() {
        //console.log ('in WordmakerController.onInit()...');
           
        this.setInfoListViewRef( { 
            xtype: 'informationlistview',
            title: 'information'
        });
    },
           
    // current entries 'isEditable' flag
    //
    onItemTap : function(list, index) {
        // Sencha bug allows the row to be tapped more than once, so check for this
        var innerItems = this.getMain().getInnerItems();
        if ( innerItems.length > 1 ) {
           console.log('double tap..' + innerItems.length);
           return;
        } 
           
        //console.log('index = ' + index);
        switch(index) {
           // if this is the dictionary list view then add entry
           case 0:
                this.getMain().push({xtype: 'howtouseappinfoview'});
                break;
           case 1:
                this.getMain().push({xtype: 'languagefootprintinfoview'});
                break;
           case 2:
                this.getMain().push({xtype: 'soundsofiwaidjainfoview'});
                break;
           case 3:
                this.getMain().push({xtype: 'aboutthisappinfoview'});
                break;
        }
    }
   
});
